/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cubelabs.nexus.controller;

import cubelabs.nexus.dao.GuideInfoDAO;
import cubelabs.nexus.dao.HelpMeDTO;
import cubelabs.nexus.entity.CounsellingAcceptor;
import cubelabs.nexus.entity.CounsellingEvent;
import cubelabs.nexus.entity.GuideInfo;
import cubelabs.nexus.entity.User;
import cubelabs.nexus.service.GuideService;
import cubelabs.nexus.service.NexusService;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author chaitanya
 */
@Controller
public class GuideController {
	private static final Logger logger = LoggerFactory.getLogger(GuideController.class);
	
	@Autowired
	private GuideService guideService;
	
	@Autowired
	private NexusService nexusService;
	
	@RequestMapping(value = "/guideTab", method = RequestMethod.GET)
	public ModelAndView guideTab(Locale locale, Model model){
		
		Map map = new HashMap();
		map.put("command", new GuideInfo());
		map.put("categories",guideService.getCategories());
		return new ModelAndView("guideTab",map);
	}
	
	@RequestMapping(value = "/addGuideInfo", method = RequestMethod.POST)
	public ModelAndView addGuideInfo(@ModelAttribute("guideInfo") GuideInfo guideInfo, Locale locale, Model model,HttpSession session){
		guideInfo.setUser((User)session.getAttribute("user"));
		guideService.save(guideInfo);
		ModelAndView mv = new ModelAndView("myprofile");
		User user = (User)session.getAttribute("user");
		mv.addObject("cap",guideService.findAllCapbilities(user.getId()));
		mv.addObject("profile", nexusService.findProfile(user.getId()));
		return mv;
	}
	
	@RequestMapping(value="/capabilities")
	public @ResponseBody List<GuideInfo> getGuidingCapabilities(@ModelAttribute("userId") long userId){
		return guideService.findAllCapbilities(userId);
	}
	
	@RequestMapping(value = "/helpme", method = RequestMethod.POST)
	public ModelAndView helmMe(@ModelAttribute("helpFrom") long helpFrom ,Locale locale, Model model,HttpSession session){
		
		Map map = new HashMap();
		HelpMeDTO hmd=new HelpMeDTO();
		hmd.setHelpFrom(helpFrom);
		map.put("command", hmd);
		return new ModelAndView("helpme",map);
	}
	@RequestMapping(value = "/requestHelp", method = RequestMethod.POST)
	public ModelAndView requestHelp(@ModelAttribute HelpMeDTO hmd ,Locale locale, Model model,HttpSession session){
		guideService.save((User)session.getAttribute("user"), hmd);
		return new ModelAndView("helpRequested");
	}
	
	@RequestMapping(value="/pendingRequests")
	public ModelAndView pendingRequests(Locale locale, Model model,HttpSession session){
		List<CounsellingAcceptor> list =guideService.findPendingRequests((User)session.getAttribute("user"));
		Map map = new HashMap();
		map.put("pendingRequests", list);
		return new ModelAndView("pendingRequests",map);
	}
	
	@RequestMapping(value="/acceptHelpRequest")
	public ModelAndView acceptHelpRequest(@ModelAttribute("coAcId") long caid, Locale locale, Model model,HttpSession session){
		User user=(User)session.getAttribute("user");
		
		boolean result = false;;
		 if(guideService.acceptHelpRequest(caid,user)){
				result = true;	 
			 }else{
				 result = false;
			 }
		
		List<CounsellingAcceptor> list =guideService.findPendingRequests((User)session.getAttribute("user"));
		Map map = new HashMap();
		map.put("pendingRequests", list);
		return new ModelAndView("pendingRequests",map);
		
		
	}
	@RequestMapping(value="/rejectHelpRequest")
	public ModelAndView rejectHelpRequest(@ModelAttribute("coAcId") long caid, Locale locale, Model model,HttpSession session){
		User user=(User)session.getAttribute("user");
		boolean result = false;;
		 if(guideService.acceptHelpRequest(caid,user)){
				result = true;	 
			 }else{
				 result = false;
			 }
			List<CounsellingAcceptor> list =guideService.findPendingRequests((User)session.getAttribute("user"));
			Map map = new HashMap();
			map.put("pendingRequests", list);
			return new ModelAndView("pendingRequests",map);
		
	}
	
}
